﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TfsGroupMembershipVisualizer
{
    public partial class Form1 : Form
    {
        private ProjectInfo[] projects;
        private TfsTeamProjectCollection collection;
        private string iconPath;
        public Form1(string iconPath)
        {
            InitializeComponent();
            this.iconPath = iconPath;
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            var picker = new TeamProjectPicker(TeamProjectPickerMode.MultiProject, false);
            if (picker.ShowDialog() == DialogResult.OK)
            {
                projects = picker.SelectedProjects;
                collection = picker.SelectedTeamProjectCollection;

                listBox1.Items.Clear();

                listBox1.Items.AddRange(projects.Select(p => p.Name).ToArray());
            }
        }

        public XDocument Document { get; private set; }

        private void btnGenerateGraph_Click(object sender, EventArgs e)
        {
            var generator = new TfsMembershipGraphGenerator();

            var document = generator.GenerateDependencyGraph(collection.Uri, projects.Select(p => p.Uri).ToArray(), iconPath);
            Document = document;
            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
